<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="width: 100%">
      <!-- 搜索框，添加商品按钮 -->
      <el-row>
        <el-col :span="8">
          <el-input
            v-model="searchInput"
            placeholder="请输入内容"
            clearable
            @clear="serchGoods()"
            @change="serchGoods()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="serchGoods()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button class="addGoodsBtn" type="primary" @click="addGoods()"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="goodsList" border stripe class="tablefontsize">
        <el-table-column label="#" type="index" align="center" width="40">
        </el-table-column>
        <el-table-column label="商品名称" prop="goods_name"> </el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="90">
        </el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="80">
        </el-table-column>
        <el-table-column label="创建时间" width="140">
          <template v-slot:default="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <!-- 操作，修改和删除两个按钮 -->
        <el-table-column label="操作" width="120">
          <template v-slot:default="scope">
            <el-button type="primary" @click="editGoods(scope.row)" size="mini">
              <i class="el-icon-edit"> </i>
            </el-button>
            <el-button
              type="danger"
              @click="deleteGoods(scope.row)"
              size="mini"
            >
              <i class="el-icon-delete"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页显示条 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 20, 30]"
        background
        :page-size="queryInfo.pagesize"
        :current-page="queryInfo.pagenum"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 搜索框输入的内容
      searchInput: '',
      goodsList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get(
        'goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      } else {
        this.goodsList = res.data.goods
        this.total = res.data.total
      }
    },
    serchGoods () {
      this.queryInfo.pagenum = 1
      this.queryInfo.query = this.searchInput
      this.getGoodsList()
    },
    addGoods () {
      this.$router.push('/goods/add')
    },
    // 每页显示数量改变
    handleSizeChange (newPageSize) {
      this.queryInfo.pagesize = newPageSize
      this.getGoodsList()
    },
    // 页码改变
    handleCurrentChange (newPageNum) {
      this.queryInfo.pagenum = newPageNum
      this.getGoodsList()
    },
    editGoods (row) {
      // 还没做呢，别忘了做(待做:关键词搜索待做)
    },
    deleteGoods (row) {
      this.$MessageBox.confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async () => {
        // 发送请求，进行删除用户操作
        const { data: res } = await this.$http.delete(`goods/${row.goods_id}`)
        if (res.meta.status === 200) {
          this.$message.success(res.meta.msg)
          this.getGoodsList()
        } else {
          this.$message.error(res.meta.msg)
        }
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  }
}
</script>

 <style lang="less" scoped>
.addGoodsBtn {
  margin-left: 20px;
}
.tablefontsize {
  font-size: 10px;
}
</style>
